package test.my.com.myapplicationtest.paint_view;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by ThinkPad on 2016/3/15.
 */
public class BitmapUtil {

    /**
     * 获取压缩后图片的bitmap
     *
     * @param path
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    public static Bitmap createBitmapByPath(String path, int reqWidth, int reqHeight) {

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        options.inSampleSize = calateBitmapSampleSize(options, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;

        Bitmap bitmap = BitmapFactory.decodeFile(path, options).copy(Bitmap.Config.ARGB_4444, true);
        return resizeBitmap(bitmap, reqWidth, reqHeight);
    }

    /**
     * 获取压缩后图片的bitmap
     *
     * @param path
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    public static Bitmap createOriginalBitmapByPath(String path, int reqWidth, int reqHeight) {

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        options.inSampleSize = calateBitmapSampleSize(options, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;

        return BitmapFactory.decodeFile(path, options).copy(Bitmap.Config.ARGB_8888, true);
    }

    private static int calateBitmapSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        if (reqWidth == 0 || reqHeight == 0) {
            return 1;
        }
        final int bitmapWidth = options.outWidth;
        final int bitmapHeight = options.outHeight;
        int inSampleSize = 1;

        if (bitmapWidth > reqWidth || bitmapHeight > reqHeight) {
            final int halfWidth = bitmapWidth / 2;
            final int halfHeight = bitmapHeight / 2;

            while ((halfWidth / inSampleSize) >= reqWidth && (halfHeight / inSampleSize) >= reqHeight) {
                inSampleSize *= 2;
            }
        }
        return inSampleSize;
    }

    /**
     * 根据需要宽高重新设置图片宽高
     *
     * @param bitmap
     * @param w      需要的宽
     * @param h      需要的高
     * @return
     */
    private static Bitmap resizeBitmap(Bitmap bitmap, int w, int h) {
        if (bitmap != null) {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            int newWidth = w;
            int newHeight = h;
            float scaleWight = (float) newWidth / width;
            float scaleHeight = (float) newHeight / height;
            float scale = scaleHeight < scaleWight ? scaleHeight : scaleWight;
            Matrix matrix = new Matrix();
            matrix.postScale(scale, scale);
            Bitmap res = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
            return res;

        } else {
            return null;
        }
    }

    /**
     * 保存操作后图片
     *
     * @param bitmap
     * @param desPath 目标路径
     * @return
     */
    public static boolean createImage(Bitmap bitmap, String desPath) {
        boolean isCreateOk = false;
        ByteArrayOutputStream baos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            file = new File(desPath);
            if(file.exists()){
                file.delete();
            }
            fos = new FileOutputStream(file);
            baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            int options = 100;
            //循环判断如果压缩后图片是否大于100kb,大于继续压缩
            while (baos.toByteArray().length / 1024 > 200) {
                //重置baos即清空baos
                baos.reset();
                //这里压缩options%，把压缩后的数据存放到baos中
                bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
                //每次都减少10
                options -= 10;
            }
            byte[] b = baos.toByteArray();
            if (b != null) {
                fos.write(b);
            }
            isCreateOk = true;
        } catch (IOException e) {
            e.printStackTrace();
            isCreateOk = false;
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
                if (baos != null) {
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                isCreateOk = false;
            }
        }
        return isCreateOk;
    }

    /**
     * 删除已经不用的图片
     * @param path
     */
    public static void deleteUselessPic(String path){
        File file = new File(path);
        if(file.exists()){
            file.delete();
        }
    }

}
